import Vue from 'vue'
import Cookies from 'vue-cookies'

class Core {
  // 存 Cookie
  SET_COOKIE (backData) {
    Object.keys(backData).forEach(key => {
      Cookies.set(key, backData[key])
    })
  }

  // 取 Cookie
  GET_COOKIE (key) {
    return Cookies.get(key)
  }

  // 删 Cookie
  DEL_COOKIE (key) {
    return Cookies.remove(key)
  }

  // 展 Cookie
  LIST_COOKIE () {
    return Cookies.keys()
  }

  // is key
  ISKEY_COOKIE (key) {
    return Cookies.isKey(key)
  }

  // 存 sessionStorage
  SET_SESSIONSTORAGE (key, value) {
    if (typeof value === 'string') {
      return window.sessionStorage.setItem(key, value)
    } else {
      return window.sessionStorage.setItem(key, JSON.stringify(value))
    }
  }

  // 取 sessionStorage
  GET_SESSIONSTORAGE (key) {
    return window.sessionStorage.getItem(key)
  }

  // 删 sessionStorage
  DEL_SESSIONSTORAGE (key) {
    return window.sessionStorage.removeItem(key)
  }

  // is 路由权限
  IS_JURISDICTION (path) {
    // const pathMap = store.state.menuTabs.pathMap
    // const pathObj = pathMap[path]
    // return pathObj && pathObj.authority
    return true
  }

  // 是否为空
  IS_EMPTY (data) {
    if (data === 0) {
      return false
    }
    if (!data) {
      return true
    }
    if (Array.isArray(data) && !data.length) {
      return true
    }
    if (typeof data === 'object') {
      for (let item in data) {
        if (data[item] || data[item] === 0) {
          return false
        }
      }
    }
    return true
  }

  // 简单数组去重
  sole (arr) {
    return Array.from(new Set(arr))
  }

  // 克隆
  clone (val) {
    return JSON.parse(JSON.stringify(val))
  }
}

Vue.prototype.$Core = new Core()

export default new Core()
